﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NerdDinner.Models
{
    public class DinnerRepository
    {

        private NerdDinnerDataContext repository = new NerdDinnerDataContext();

        //
        //  Query methods

        public IQueryable<Dinner> FindAllDinners()
        {
            return repository.Dinners;
        }

        public IQueryable<Dinner> FindUpcommingDinners()
        {
            return from dinner in repository.Dinners
                   where dinner.EventDate > DateTime.Now
                   orderby dinner.EventDate
                   select dinner;
        }

        public Dinner GetDinner(int id)
        {
            return repository.Dinners.SingleOrDefault(d => d.DinnerID == id);
        }

        //
        //  Insert/Delete methods
        public void Add(Dinner dinner)
        {
            repository.Dinners.InsertOnSubmit(dinner);
        }

        public void Delete(Dinner dinner)
        {
            repository.RSVPs.DeleteAllOnSubmit(dinner.RSVPs);
            repository.Dinners.DeleteOnSubmit(dinner);
        }

        //
        //  Persistance
        public void Save()
        {
            repository.SubmitChanges();
        }
    }
}
